﻿var dialogManager = function () {
    this.template = '<div class="dialog" style="top:80px; left:100px;position:absolute">\
	                    <div ratio="#28" class="header" onselectstart="return false;" oncopy="return false;" onpaste="return false;" oncut="return false;">\
		                    <div class="dhr">\
			                    <div class="dhc">\
				                    <a class="close" href="#close">close</a>\
				                    <a class="maximize" href="#maximize">maximize</a>\
				                    <a class="restore" href="#restore">restore</a>\
				                    <a class="minimize" href="#minimize">minimize</a>\
				                    <h1>弹出窗口</h1>\
			                    </div>\
		                    </div>\
	                    </div>\
	                    <div class="content" ratio="*">\
                            <div class="contentwarp"></div>\
	                    </div>\
	                    <div ratio="#5" class="footer"><div class="dfr"><div class="dfc"></div></div></div>\
                    </div>';
    this.open = function (option) {
        option = $.extend({
            type: "iframe", //ajax,html
            title: "未命名窗口",
            id: "dialog_" + new Date().getTime(),
            maskid: "mask_dialog_" + new Date().getTime(),
            frompointerid: mWindowPointer.id(),
            intent:null,
            mask: true,
            zindex: null,
            maskzindex:null,
            parent: "body",
            resizeable: true,
            dragable: true,
            maxable: true,
            minable: false,
            width: "90%",
            height: "90%",
            content: null,//html
            url: null,//iframe
            data: null,//ajax
            onclose:function(){}
        }, option);
        if (option.maskzindex == null) {
            option.maskzindex =mZindexManager.getTop(option.maskid);
        }
        if (option.zindex == null) {
            option.zindex = mZindexManager.getTop(option.id);
        }
        var dialog = $(this.template);
        var helper = new $.dialogHelper(dialog, option);
        dialog.helper("dialog", helper);
        helper.init();
        return helper;
    };
};

(function ($) {
    $.dialogHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;
        this.maxbutton = null;
        this.restorebutton = null;
        this.closebutton = null;
        this.minbutton = null;
        this.content = null;
        this.mask=null;
        var _this = this;
        this.init = function () {
            this.jo.attr("dialogid", this.setting.id);
            this.jo.attr("id", this.setting.id);
            this.jo.find("h1").html(this.setting.title);
            this.jo.css("width", this.setting.width);
            this.jo.css("height", this.setting.height);
            this.jo.css("z-index", this.setting.zindex);
            this.maxbutton = this.jo.find(".maximize");
            this.restorebutton = this.jo.find(".restore");
            this.closebutton = this.jo.find(".close");
            this.minbutton = this.jo.find(".minimize");
            this.content = this.jo.find(".content");
            this.restorebutton.hide();
            
            this.closebutton.click(function () {
                _this.close();
            });
            this.maxbutton.click(function () {
                _this.maximize();
            });
            this.minbutton.click(function () {
                _this.minimize();
            });
            this.restorebutton.click(function () {
                _this.restore();
            });
            if(this.setting.mask){
                this.mask=$("<div class=\"mask\"></div>");
                this.mask.attr("id",this.setting.maskid);
                this.mask.attr("maskid", this.setting.maskid);
                this.mask.css("z-index",this.setting.maskzindex);
                $(this.setting.parent).append(this.mask);
            }
            $(this.setting.parent).append(this.jo);
            this.center();
            this.jo.verticalSplitContainer();
            if (this.setting.type == "html") {
                this.html(this.setting.content);
            } else if(this.setting.type=="iframe") {
                this.html("<div id='_hyt_loading' style='width:100%;text-align:center;'><img src='/Resources/project/images/loading.gif'/></div>");
                Utils.loadUrlto(this.setting.url, this.content, this.setting.frompointerid,intent, function (iframe) { 
                    $(this).parent().find("#_hyt_loading").remove(); 
                    iframe.contentWindow.mWindowPointer.listen(intent.action.closewindow, function (_pointer, _arg) {
                        _this.close();
                    });
                    //_this.jo.layout();
                    //_this.center();
                    //_this.jo.layout();
                });
            }else if(this.setting.type=="ajax") {
                this.load(this.setting.data);
            }

            this.setting.dragable&&this.jo.dragable({handlerSlt:".header",hideonmove:".content .contentwarp"});
            this.setting.resizeable&&this.jo.resizeable();
            this.setting.maxable||this.maxbutton.hide();
            this.setting.minable||this.minbutton.hide();
            this.jo.mousedown(function(){
                _this.focus();
            });
        };
        this.html=function(content){
            this.content.html(content);
        };
        this.center=function(){
            var winWidth=$(window).width();
            var winHeight=$(window).height();
            var thisWidth=this.jo.width();
            var thisHeight=this.jo.height();
            var left=(winWidth-thisWidth)/2;
            if(left<=0)left=0;
            var top=(winHeight-thisHeight)/2;
            if(top<=0)top=0;
            this.jo.css({left:left+"px",top:top+"px"});
        };
        this.load=function(data){
            var st = {
                 data:data
            };
            mAjaxManager.load(this.content.children(),st);
        };
        this.focus=function(){
            var zindex=mZindexManager.moveTotop(this.setting.id);            
            this.jo.css("z-index",zindex);
        };
        this.close = function () {
            if(this.setting.type=="iframe") {
                try{
                var pointerid=this.content.find("iframe").get(0).contentWindowPointerid;
                 var p = mPointerManager.get(pointerid);
                 
                if (p) {
                    p.release();
                }
                //mPointerManager.remove(pointerid);
                }catch(ex){}
            }
            mZindexManager.remove(this.setting.id);
            this.jo.remove();
            this.mask&&this.mask.remove();
            this.setting.onclose();
            if(this.setting.frompointerid){
                var frompointer=mPointerManager.get(this.setting.frompointerid);
                if(frompointer){
                    frompointer.notify(intent.action.focus);
                }
            }
        };
        this.maximize = function () {
            this.maxbutton.hide();
            this.restorebutton.show();
            this.oldwidth=this.jo.width();
            this.oldheight=this.jo.height();
            this.oldx=this.jo.position().left;
            this.oldy=this.jo.position().top;
            var width=Math.max(this.jo.parent().width(),$(document).width());
            var height=Math.max(this.jo.parent().height(),$(document).height());
            var x=$(document).scrollLeft();
            var y=$(document).scrollTop();
            this.jo.css({left:x+"px",top:y+"px"});
            this.jo.width(width);
            this.jo.height(height);
            this.jo.helper("resizeable")&&this.jo.helper("resizeable").disable();
            this.jo.helper("dragable")&&this.jo.helper("dragable").disable();
            this.jo.layout();
        };
        this.restore = function () {
            this.maxbutton.show();
            this.restorebutton.hide();
            this.jo.css({left:this.oldx+"px",top:this.oldy+"px"});
            this.jo.width(this.oldwidth);
            this.jo.height(this.oldheight);
            this.jo.helper("resizeable")&&this.jo.helper("resizeable").enable();
            this.jo.helper("dragable")&& this.jo.helper("dragable").enable();
            this.jo.layout();
        };
    };
})(jQuery);
var mDialogManager=new dialogManager();